package vip.xiaonuo.miniapp.api.listener;

import static reactor.event.selector.Selectors.$;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.sms.SmsSender;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.mapper.OrgDeviceMapper;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;
import vip.xiaonuo.sys.modular.sms.enums.SmsSendSourceEnum;
import vip.xiaonuo.sys.modular.sms.enums.SmsTypeEnum;
import vip.xiaonuo.sys.modular.sms.param.SysSmsSendParam;
import vip.xiaonuo.sys.modular.sms.service.SmsSenderService;

/**
 *  小程序邀请人员发送短信通知
 */
@Service
@Slf4j
public class VisitorMiniAppAddListener implements Consumer<Event<Long>>, InitializingBean {

    @Autowired
    private Reactor reactor;

    @Autowired
    private VisitorService visitorService;

    @Resource
    private SmsSenderService smsSenderService;

    @Override
    public void accept(Event<Long> event) {
        Visitor visitor = visitorService.getById(event.getData());
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("name", visitor.getInviterName());
        objectObjectHashMap.put("visitorId", visitor.getId());
        SysSmsSendParam sysSmsSendParam = new SysSmsSendParam();
        sysSmsSendParam.setSmsSendSourceEnum(SmsSendSourceEnum.APP);
        sysSmsSendParam.setSmsTypeEnum(SmsTypeEnum.MESSAGE);
        sysSmsSendParam.setTemplateCode("SMS_265560116");
        sysSmsSendParam.setPhoneNumbers(visitor.getTel());
        sysSmsSendParam.setParams(objectObjectHashMap);
        smsSenderService.sendShortMessage(sysSmsSendParam);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.VISITOR_MINIAPP_SEND_MSG), this);
    }
}
